package de.voichie.android.cipher.activities;

import de.voichie.android.cipher.R;
import de.voichie.android.cipher.business.Crypter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DecryptActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.decrypt_layout);
        
//       --- decrypt password --------
        findViewById(R.id.btnStartDecryption).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                
            	// get the user input of password in crypted form
            	EditText editTextCryptedPassword = (EditText) findViewById(R.id.editTextInputCiphedPassword);
            	
            	if (editTextCryptedPassword.getText() == null || editTextCryptedPassword.getText().toString() == null || editTextCryptedPassword.getText().toString().equals(""))
            	{
            		// TODO message to user, that he has to enter a crypted password
            		return;
            	}
            	String cryptedPassword = editTextCryptedPassword.getText().toString();
            	
            	// decrypt the password with business logic
            	String deryptedPassword = createDecryptedPassword(cryptedPassword);
            	
            	// display the decrypted password to the user
            	((EditText) findViewById(R.id.editTextOutputDechiphedPassword)).setText(deryptedPassword);
            	((EditText) findViewById(R.id.editTextOutputDechiphedPassword)).setVisibility(View.VISIBLE);
            	((TextView) findViewById(R.id.textDechiphredPassword)).setVisibility(View.VISIBLE);
            	
            }
        });
        
//        --- back is pressed ------------
        findViewById(R.id.btnBackToLastActivity).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	onBackPressed();
            }
        });
        
    }
    
    private String createDecryptedPassword (String cryptedPassword) {
    	return Crypter.getCrypterSingleton().decryptPassword(cryptedPassword);
    }
}